<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="护理人员" prop="nurseName">
        <el-input
          v-model="queryParams.nurseName"
          placeholder="请输入护理人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请假类型" prop="leaveType">
        <el-select
          v-model="queryParams.leaveType"
          placeholder="请选择请假类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_leave_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="请假时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_leave_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="hasRole('nurse')">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >申请请假</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="nurseLeaveList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="请假编号" align="center" prop="leaveId" />
      <el-table-column label="护理人员" align="center" prop="nurseName" />
      <el-table-column label="请假类型" align="center" prop="leaveType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_leave_type"
            :value="scope.row.leaveType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.startTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="请假原因"
        align="center"
        prop="reason"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_leave_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:nurseLeave:edit']"
            v-if="scope.row.status === '0'"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-show="scope.row.status == '0' && hasRole('nurse')"
            v-hasPermi="['system:nurseLeave:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAudit(scope.row)"
            v-show="scope.row.status == '0' && hasRole('rs')"
            v-hasPermi="['system:leave:audit']"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改护理人员请假对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请假类型" prop="leaveType">
          <el-select v-model="form.leaveType" placeholder="请选择请假类型">
            <el-option
              v-for="dict in dict.type.sys_leave_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请假原因" prop="reason">
          <el-input
            v-model="form.reason"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看护理人员请假对话框 -->
    <el-dialog
      title="查看请假详情"
      :visible.sync="viewOpen"
      width="500px"
      append-to-body
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="请假编号">{{
          viewForm.leaveId
        }}</el-descriptions-item>
        <el-descriptions-item label="护理人员">{{
          viewForm.nurseName
        }}</el-descriptions-item>
        <el-descriptions-item label="请假类型">
          <dict-tag
            :options="dict.type.sys_leave_type"
            :value="viewForm.leaveType"
          />
        </el-descriptions-item>
        <el-descriptions-item label="开始时间">{{
          parseTime(viewForm.startTime, "{y}-{m}-{d} {h}:{i}:{s}")
        }}</el-descriptions-item>
        <el-descriptions-item label="结束时间">{{
          parseTime(viewForm.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
        }}</el-descriptions-item>
        <el-descriptions-item label="请假原因">{{
          viewForm.reason
        }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <dict-tag
            :options="dict.type.sys_leave_status"
            :value="viewForm.status"
          />
        </el-descriptions-item>
        <el-descriptions-item label="审核人" v-if="viewForm.auditBy">{{
          viewForm.auditBy
        }}</el-descriptions-item>
        <el-descriptions-item label="审核时间" v-if="viewForm.auditTime">{{
          parseTime(viewForm.auditTime, "{y}-{m}-{d} {h}:{i}:{s}")
        }}</el-descriptions-item>
        <el-descriptions-item label="审核备注" v-if="viewForm.auditRemark">{{
          viewForm.auditRemark
        }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{
          parseTime(viewForm.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
        }}</el-descriptions-item>
        <el-descriptions-item label="备注" v-if="viewForm.remark">{{
          viewForm.remark
        }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核护理人员请假对话框 -->
    <el-dialog
      title="审核护理人员请假"
      :visible.sync="auditOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="auditForm"
        :model="auditForm"
        :rules="auditRules"
        label-width="80px"
      >
        <el-form-item label="护理人员">
          <span>{{ auditForm.nurseName }}</span>
        </el-form-item>
        <el-form-item label="请假类型">
          <dict-tag
            :options="dict.type.sys_leave_type"
            :value="auditForm.leaveType"
          />
        </el-form-item>
        <el-form-item label="开始时间">
          <span>{{
            parseTime(auditForm.startTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </el-form-item>
        <el-form-item label="结束时间">
          <span>{{
            parseTime(auditForm.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </el-form-item>
        <el-form-item label="请假原因">
          <span>{{ auditForm.reason }}</span>
        </el-form-item>
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="auditForm.status">
            <el-radio label="1">同意</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input
            v-model="auditForm.auditRemark"
            type="textarea"
            placeholder="请输入审核备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAuditForm">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNurseLeave,
  getNurseLeave,
  delNurseLeave,
  addNurseLeave,
  updateNurseLeave,
  auditNurseLeave,
} from "@/api/system/nurseLeave";

export default {
  name: "NurseLeave",
  dicts: ["sys_leave_type", "sys_leave_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 护理人员请假表格数据
      nurseLeaveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      viewOpen: false,
      // 是否显示审核弹出层
      auditOpen: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nurseName: null,
        leaveType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 查看表单参数
      viewForm: {},
      // 审核表单参数
      auditForm: {
        status: "1",
        auditRemark: "",
      },
      // 表单校验
      rules: {
        leaveType: [
          { required: true, message: "请假类型不能为空", trigger: "change" },
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
          { validator: this.validateEndTime, trigger: "blur" },
        ],
        reason: [
          { required: true, message: "请假原因不能为空", trigger: "blur" },
        ],
      },
      // 审核表单校验
      auditRules: {
        status: [
          { required: true, message: "审核结果不能为空", trigger: "change" },
        ],
        auditRemark: [
          { required: true, message: "审核备注不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 判断当前用户是否具有指定角色
    hasRole(role) {
      return this.$store.getters && this.$store.getters.roles.includes(role);
    },
    // 校验结束时间必须大于开始时间
    validateEndTime(rule, value, callback) {
      if (this.form.startTime && value) {
        if (new Date(value) <= new Date(this.form.startTime)) {
          callback(new Error("结束时间必须大于开始时间"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    },
    /** 查询护理人员请假列表 */
    getList() {
      this.loading = true;
      listNurseLeave(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.nurseLeaveList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消审核按钮
    cancelAudit() {
      this.auditOpen = false;
      this.resetAuditForm();
    },
    // 表单重置
    reset() {
      this.form = {
        leaveId: null,
        leaveType: null,
        startTime: null,
        endTime: null,
        reason: null,
        remark: null,
      };
      this.resetForm("form");
    },
    // 审核表单重置
    resetAuditForm() {
      this.auditForm = {
        leaveId: null,
        nurseName: null,
        leaveType: null,
        startTime: null,
        endTime: null,
        reason: null,
        status: "1",
        auditRemark: "",
      };
      this.resetForm("auditForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.leaveId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "申请请假";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const leaveId = row.leaveId || this.ids;
      getNurseLeave(leaveId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改护理人员请假";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      const leaveId = row.leaveId;
      getNurseLeave(leaveId).then((response) => {
        this.viewForm = response.data;
        this.viewOpen = true;
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.resetAuditForm();
      const leaveId = row.leaveId;
      getNurseLeave(leaveId).then((response) => {
        this.auditForm = response.data;
        this.auditForm.status = "1"; // 默认同意
        this.auditForm.auditRemark = "";
        this.auditOpen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.leaveId != null) {
            updateNurseLeave(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNurseLeave(this.form).then((response) => {
              this.$modal.msgSuccess("申请成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交审核表单 */
    submitAuditForm() {
      this.$refs["auditForm"].validate((valid) => {
        if (valid) {
          auditNurseLeave(this.auditForm.leaveId, {
            status: this.auditForm.status,
            auditRemark: this.auditForm.auditRemark,
          }).then((response) => {
            this.$modal.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leaveIds = row.leaveId || this.ids;
      this.$modal
        .confirm('是否确认删除护理人员请假编号为"' + leaveIds + '"的数据项？')
        .then(function () {
          return delNurseLeave(leaveIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/nurse/leave/export",
        {
          ...this.queryParams,
        },
        `nurse_leave_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
